package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.ApplicationVariable;
import com.example.demo.common.StringTools;
import com.example.demo.common.UserSessionTools;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.vo.ArticleInfoVO;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: cheng
 * @date: 2023/6/25 14:27
 * @description:
 */
@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo, HttpServletRequest request){
        if(null == articleInfo || !StringUtils.hasLength(articleInfo.getTitle()) ||
                                  !StringUtils.hasLength(articleInfo.getContent())) {
            return AjaxResult.fail(-1, "参数错误");
        }
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        int result = articleService.add(articleInfo);
        return AjaxResult.success(result);
    }

    /**
     * 获取文章详情信息, 但需要鉴权(判断当前文章是否属于当前登录用户)
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/getdetailbyid")
    public AjaxResult getDetailByIdAndUid(Integer id,HttpServletRequest request){
        if(id == null || id <= 0){
            return AjaxResult.fail(-1,"非法参数");
        }
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        return AjaxResult.success(articleService.getDetailByIdAndUid(id, userInfo.getId()));
    }

    /**
     * 获取文章详情后进行修改
     * @param articleInfo
     * @param request
     * @return
     */
    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo,HttpServletRequest request){
        if(null == articleInfo || articleInfo.getId() <= 0 ||
                !StringUtils.hasLength(articleInfo.getTitle()) ||
                !StringUtils.hasLength(articleInfo.getContent())){
            return AjaxResult.fail(-1,"参数错误");
        }
        //获取登录用户 id
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        int result = articleService.update(articleInfo);
        return AjaxResult.success(result);
    }

    /**
     * 查看文章正文
     * @param id
     * @return
     */
    @RequestMapping("/getdetail")
    public AjaxResult getDetail(Integer id){
        if(null == id || id <= 0){
            return AjaxResult.fail(-1," 参数有误");
        }
        ArticleInfoVO articleInfoVO = articleService.getDetail(id);
        return AjaxResult.success(articleInfoVO);
    }

    /**
     * 阅读量
     * @param id
     * @return
     */
    @RequestMapping("/addrcount")
    public AjaxResult addRCount(Integer id){
        if(id == null || id <= 0){
            return AjaxResult.fail(-1,"参数错误");
        }
        int result = articleService.addRCount(id);
        return AjaxResult.success(result);
    }

    /**
     * 登陆状态查看文章列表
     * @param request
     * @return
     */
    @RequestMapping("/mylist")
    public AjaxResult getListByUid(HttpServletRequest request){
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        List<ArticleInfo> list = articleService.getListByUid(userInfo.getId());
        for(ArticleInfo item : list){
            String count = StringTools.subLength(item.getContent(),ApplicationVariable.TEXT_ABSTRACTS);
            item.setContent(count);
        }
        return AjaxResult.success(list);
    }

    /**
     * 删除文章
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/del")
    public AjaxResult del(Integer id,HttpServletRequest request){
        if(null == id || id <= 0){
            return AjaxResult.fail(-1,"参数有误");
        }
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        int result = articleService.del(id,userInfo.getId());
        return AjaxResult.success(result);
    }

    /**
     *
     * @param pageSize 共有多少文章
     * @param pageIndex 页码
     * @return
     */
    @RequestMapping("/getlistbypage")
    public AjaxResult getListByPage(Integer pageSize,Integer pageIndex){
        if(pageSize == null || pageSize <= 0){
            pageSize = 2;
        }
        if(pageIndex == null || pageIndex < 1){
            pageIndex = 1;
        }
        //分页公式
        int offset= (pageIndex - 1) * pageSize;
        List<ArticleInfo> list = articleService.getListByPage(pageSize,offset);
        list.stream().parallel().forEach(item ->{
            item.setContent(StringTools.subLength(item.getContent(),ApplicationVariable.TEXT_ABSTRACTS));
        });
        return AjaxResult.success(list);
    }

    @RequestMapping("/getcount")
    public AjaxResult getCount(){
        return AjaxResult.success(articleService.getCount());
    }

//    @RequestMapping("/getnumber")
//    public AjaxResult getArtNumber(HttpServletRequest request){
//        UserInfo userInfo = UserSessionTools.getLoginUser(request);
//        int id = userInfo.getId();
//        if(id <= 0){
//            return AjaxResult.fail(-1," 参数有误");
//        }
//        int result = articleService.getArtNumber(id);
//        return AjaxResult.success(result);
//    }
}
